//
//  NSString+Encryption.m
//  YTEncryption
//
//  Created by YaTang on 2017/7/19.
//  Copyright © 2017年 YaTang. All rights reserved.
//

#import "NSString+Encryption.h"
#import <CommonCrypto/CommonDigest.h>
#import "DGGDES3Util.h"

@implementation NSString (Encryption)

- (NSString *)MD5String{
    NSAssert(self, @"string is nil!");
    const char *cStr = [self UTF8String];
    unsigned char digest[CC_MD5_DIGEST_LENGTH];
    CC_MD5( cStr, (CC_LONG)strlen(cStr), digest );
    NSMutableString *output = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
    
    for(int i = 0; i < CC_MD5_DIGEST_LENGTH; i++)
    [output appendFormat:@"%02x", digest[i]];
    
    return output;
}

- (NSString *)MD5StringForUppercase{
    return [[self MD5String] uppercaseString];
}

- (NSString *)encryptUseDESWithKey:(NSString *)key{
    NSAssert(self, @"string is nil!");
    NSAssert(key, @"encryptKey must not be nil!");
    return [DGGDES3Util encryptUseDES:self key:key];
}

- (NSString *)decryptUseDESWithKey:(NSString *)key{
    NSAssert(self, @"string is nil!");
    NSAssert(key, @"decryptKey must not be nil!");
    return [DGGDES3Util decryptUseDES:self key:key];
}

@end
